package com.yky.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.yky.utils.base.DateUtils;

public class WeekDateUtils {

	// 返回日期范围内所有的周四？
	public static List<String> getEveryWeekByDate(String currentDate,
			String sBegin, String sEnd) {
		List<String> result = new ArrayList<String>();

		Calendar calCurrent = Calendar.getInstance();
		calCurrent.setTime(DateUtils.getDate(currentDate));

		Calendar calStart = Calendar.getInstance();
		calStart.setTime(DateUtils.getDate(sBegin));

		Calendar calEnd = Calendar.getInstance();
		calEnd.setTime(DateUtils.getDate(sEnd));

		Calendar calTmp = (Calendar) calStart.clone();
		int iLoop = 1;
		while (!calEnd.equals(calTmp)) {
			calTmp.add(Calendar.DATE, iLoop);
			if (calCurrent.getTime().getDay() == calTmp.getTime().getDay()) {
				result
						.add(DateUtils.formatDate(calTmp.getTime(),
								"yyyy-MM-dd"));
			}

		}
		return result;
	}

	// 根据当前日期向后计算出一周日期(所在学期内)
	public static List<String> getOneWeekByCurrentDate(String sBegin,
			String sEnd) {
		List<String> result = new ArrayList<String>();

		Calendar calStart = Calendar.getInstance();
		calStart.setTime(DateUtils.getDate(sBegin));

		Calendar calEnd = Calendar.getInstance();
		calEnd.setTime(DateUtils.getDate(sEnd));

		Calendar calTmp = (Calendar) calStart.clone();
		int iLoop = 1;
		int temp = 0;

		while (!calEnd.equals(calTmp)) {
			calTmp.add(Calendar.DATE, iLoop);
			result.add(DateUtils.formatDate(calTmp.getTime(), "yyyy-MM-dd")
					+ " <span>" + getWeekOfDate(calTmp.getTime()) + "</span>");
			temp += 1;
			if (temp == 7)
				break;
		}

		return result;
	}

	// 获取当前日期是星期几
	public static String getWeekOfDate(Date dt) {
		String[] weekDays = { "日", "一", "二", "三", "四", "五", "六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);

		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;

		return weekDays[w];
	}
	
	// 获取当前日期是星期几
	public static String getWeekOfDate(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = null;
		try {
			d = sdf.parse(date);
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String[] weekDays = { "日", "一", "二", "三", "四", "五", "六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	public static void main(String[] args) {
		// // 测试1
		// List<String> list = WeekDateUtils.getEveryWeekByDate("2012-08-09",
		// "2012-08-08", "2012-08-31");
		// for (int i = 0; i < list.size(); i++) {
		// System.out.println(list.get(i));
		// }

		// 测试2
		List<String> list = WeekDateUtils.getOneWeekByCurrentDate("2012-08-08",
				"2012-08-31");
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
	}

}
